package com.shell.customer.register.service.impl;

import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.cache.Cache;
import com.shell.bee.cache.CacheBuilder;
import com.shell.customer.base.service.CusCheckService;
import com.shell.customer.register.RegisterEvent;
import com.shell.customer.register.rx.SaveCusSubscriber;
import com.shell.customer.register.rx.SaveUserAndOrganSubscriber;
import com.shell.customer.register.rx.UpdateCusInvtSubscriber;
import com.shell.customer.register.service.RegisterService;
import com.shell.customer.register.vo.RegisterVo;
import com.shell.framework.utils.MessageSender;
import com.shell.framework.utils.VerifyCodeUtil;
import com.shell.governor.modules.message.keyword.SmsInviteCodeKeyword;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import rx.Observable;

@Service("registerService")
public class RegisterServiceImpl implements RegisterService {
	@Autowired
	private CusCheckService cusCheckService;
	@Autowired
	private SaveCusSubscriber saveCusSubscriber;
	@Autowired
	private UpdateCusInvtSubscriber updateCusInvtSubscriber;
	@Autowired
	private SaveUserAndOrganSubscriber saveUserAndOrganSubscriber;
	private static Cache<String, String> CACHE;

	public void customerRegister(RegisterVo registerVo) {
		String msg = this.cusCheckService.checkCusRegister(registerVo);
		if (StringUtil.notTrimEmpty(msg)) {
			throw new UnCheckMsgException(msg);
		}
		msg = checkSMSCode(registerVo.getContactorPhone(), registerVo.getMsgVerifyCode());
		if (StringUtil.notTrimEmpty(msg)) {
			throw new UnCheckMsgException(msg);
		}
		RegisterEvent registerEvent = new RegisterEvent();
		registerEvent.setRegisterVo(registerVo);

		Observable<RegisterEvent> observers = Observable.just(registerEvent);
		observers.subscribe(this.saveCusSubscriber);
		observers.subscribe(this.updateCusInvtSubscriber);

		observers.subscribe(this.saveUserAndOrganSubscriber);
	}

	private String checkSMSCode(String phoneNo, String smsCode) {
		if (CACHE == null) {
			CACHE = CacheBuilder.newBuilder().build();
		}
		String cacheSmsCode = (String) CACHE.get(phoneNo);
		if (StringUtil.emptyAndNull(cacheSmsCode)) {
			return "����������������";
		}
		if (!cacheSmsCode.equals(smsCode)) {
			return "����������������";
		}
		return "";
	}

	public String sendSMS(String phoneNo) {
		if (CACHE == null) {
			CACHE = CacheBuilder.newBuilder().build();
		}
		String smsCode = (String) CACHE.get(phoneNo);
		if (StringUtil.emptyAndNull(smsCode)) {
			smsCode = VerifyCodeUtil.generateVerifyCode(4, "0123456789");
			CACHE.put(phoneNo, smsCode, 300);
		}
		String template = "MT02103";

		SmsInviteCodeKeyword keyword = new SmsInviteCodeKeyword();
		keyword.setInviteCode(smsCode);
		try {
			MessageSender.sendMessageToPhone(phoneNo, "", template, keyword);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return smsCode;
	}
}
